//
//  WJCBaseXIBView.m
//  WJCLibrary
//
//  Created by wjc on 2022/7/21.
//

#import "WJCBaseXIBView.h"
#import "WJCDefine.h"
#import "UIImage+Extension.h"
#import "UIColor+Random.h"
@implementation WJCBaseXIBView
-(instancetype)initWithNibName:(NSString*)name{
    if (self = [super init]) {
        
        self = [[[NSBundle mainBundle] loadNibNamed:name owner:nil options:nil] firstObject];

      
    }
    return self;
}
-(void)setDeployModel:(id)deployModel{
    _deployModel = deployModel;
    [self loadAllSubViewsWithView:self];
    //        if (self.setotherCellTypeBlock) {
    //            self.setotherCellTypeBlock(self);
    //        }
}
-(void)loadAllSubViewsWithView:(UIView*)view{
    for (UIView *v in view.subviews) {
        if (v.modelDeploy.length>0) {
            [self setModelWithView:v];
        }
        if (v.subviews.count>0) {
            [self loadAllSubViewsWithView:v];
        }
    }
}
-(void)setModelWithView:(UIView*)v{
    NSArray *arr = [v.modelDeploy componentsSeparatedByString:@","];
    NSString *str;
    NSString *key;
    for (NSString *st in arr) {
        if ([st containsString:@"."]) {
            NSArray *subArr = [st componentsSeparatedByString:@"."];
            id ob = self.deployModel;
            for (NSString *s in subArr) {
                
                if ([ob checkIsExistPropertyWithInstance:ob verifyPropertyName:s andLogAllProperty:NO]) {
                    ob = [ob valueForKey:s];
                    key = s;
                }
            }
            if([ob isKindOfClass:[NSString class]]){
                str =ob;
            }
        }else{
            if ([self.deployModel isKindOfClass:[NSDictionary class]]) {
                if ([self.deployModel valueForKey:st]&&([[self.deployModel valueForKey:st] isKindOfClass:[NSString class]]||[[self.deployModel valueForKey:st] isKindOfClass:[NSNumber class]])) {
                    str =[NSString stringWithFormat:@"%@",[self.deployModel valueForKey:st]];
                    key = st;
                }
//                str =[NSString stringWithFormat:@"%@",[self.deployModel valueForKey:st]];
            }
       else if ([self.deployModel checkIsExistPropertyWithInstance:self.deployModel verifyPropertyName:st andLogAllProperty:NO]) {
            if ([[self.deployModel valueForKey:st] isKindOfClass:[NSString class]]||[[self.deployModel valueForKey:st] isKindOfClass:[NSNumber class]]) {
                str =[NSString stringWithFormat:@"%@",[self.deployModel valueForKey:st]];
                key = st;
            }
        }
        }
    }
    
        if ([v isKindOfClass:[UILabel class]]) {
            ((UILabel*)v).text = str;
            if (v.headDeploy) {
                ((UILabel*)v).text =[NSString stringWithFormat:@"%@%@",v.headDeploy, ((UILabel*)v).text];
            }
            if (v.footDeploy) {
                ((UILabel*)v).text =[NSString stringWithFormat:@"%@%@", ((UILabel*)v).text,v.footDeploy];
            }
        }else if ([v isKindOfClass:[UIButton class]]) {
            [((UIButton*)v) setTitle:str forState:UIControlStateNormal];
            if (v.headDeploy) {
                [((UIButton*)v) setTitle:[NSString stringWithFormat:@"%@%@",v.headDeploy, ((UIButton*)v).titleLabel.text] forState:UIControlStateNormal];
            }
            if (v.footDeploy) {
                [((UIButton*)v) setTitle:[NSString stringWithFormat:@"%@%@", ((UIButton*)v).titleLabel.text,v.footDeploy] forState:UIControlStateNormal];
            }
        }else if ([v isKindOfClass:[UIImageView class]]) {
            if ([UIImage imageNamed:str]) {
                ((UIImageView*)v).image = [UIImage imageNamed:str];
            }else{
                if(((UIImageView*)v).tapUrlPath.length>0&&key.length>0&&[((UIImageView*)v).tapUrlPath containsString:key]){
                    ((UIImageView*)v).tapUrlPath = str;
                }else if (!key&&((UIImageView*)v).tapUrlPath.length>0){
                    ((UIImageView*)v).tapUrlPath = nil;
                }else if (str.length<1){
                    ((UIImageView*)v).tapUrlPath = nil;
                }
//                if(v.headDeploy.length>1){
//                UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
//                label.font = Bfont(45);
//                    label.textColor = [UIColor whiteColor];
//                label.text = [v.headDeploy substringToIndex:1];
//                label.textAlignment = NSTextAlignmentCenter;
//                label.backgroundColor =[UIColor randomColor];
//                    UIImage *image = [UIImage imageWithUIView:label];
//                    [((UIImageView*)v) fc_setImageWithURL:str placeholder:image completed:nil];
//                        v.urlPath = [NSString stringWithFormat:@"%@%@",[WJCDeployModel getDeployWithDeployKey:WJCDeployImageHeadUrl],str];
//                }else{
            [((UIImageView*)v) fc_setImageWithURL:str placeholder:nil completed:nil];
                v.urlPath = [NSString stringWithFormat:@"%@%@",[WJCDeployModel getDeployWithDeployKey:WJCDeployImageHeadUrl],str];
//                }
            }
        }else if ([v isKindOfClass:[UITextField class]]) {
            ((UITextField*)v).text = str;
            if (v.headDeploy) {
                ((UITextField*)v).text =[NSString stringWithFormat:@"%@%@",v.headDeploy, ((UITextField*)v).text];
            }
            if (v.footDeploy) {
                ((UITextField*)v).text =[NSString stringWithFormat:@"%@%@", ((UITextField*)v).text,v.footDeploy];
            }
        }else if ([v isKindOfClass:[UITextView class]]) {
            ((UITextView*)v).text = str;
            if (v.headDeploy) {
                ((UITextView*)v).text =[NSString stringWithFormat:@"%@%@",v.headDeploy, ((UITextField*)v).text];
            }
            if (v.footDeploy) {
                ((UITextView*)v).text =[NSString stringWithFormat:@"%@%@", ((UITextField*)v).text,v.footDeploy];
            }
        }
        
    ///后续持续完善,
//    else{
//        if (self.idBlock) {
//            ///设置额外视图样式
//            self.idBlock(self);
//        }
//
//    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
